package com.lsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsy.dao.CountMapper;
import com.lsy.dao.GroupMapper;
import com.lsy.dao.UserMapper;
import com.lsy.dao.VoteMapper;
import com.lsy.entity.*;
import com.lsy.util.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 项目名：springboot02
 * 包名：com.lsy.controller
 * 类名：VoteController
 * 描述：
 * 作者：柳胜酉
 * 开始时间：2023/6/19
 * 完成时间：
 * 版本：1.0
 */
@Controller
public class VoteController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private VoteMapper voteMapper;
    @Autowired
    private CountMapper countMapper;

    /**
     * 默认页面
     *
     * @return 默认页面
     */
    @RequestMapping("/index")
    public String index() {
        return "/index";
    }

    /**
     * 主页面
     *
     * @return 主页面
     */
    @RequestMapping("/main")
    public String main() {
        return "/main";
    }

    /**
     * 赛制设置页
     *
     * @return 赛制设置页
     */
    @RequestMapping("/battlelist")
    public String battlelist() {
        return "battlelist";
    }

    /**
     * 屏幕展示
     *
     * @return 屏幕展示
     */
    @RequestMapping("/screen")
    public String screen() {
        return "screen";
    }

    @GetMapping("/getLatestPKOnScreen")
    @ResponseBody
    public PKCnd getLatestPKOnScreen() {
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_status", 1);
        Group group = groupMapper.selectOne(queryWrapper);
        Integer userAId = group.getUserAId();
        Integer userBId = group.getUserBId();
        User userA = userMapper.selectById(userAId);
        User userB = userMapper.selectById(userBId);
        PKCnd pkCnd = new PKCnd();
        pkCnd.setGroupId(group.getGroupId());
        pkCnd.setUserAId(userAId);
        pkCnd.setUserBId(userBId);
        pkCnd.setUsernameA(userA.getUsername());
        pkCnd.setUsernameB(userB.getUsername());
        pkCnd.setPhotoA(userA.getPhoto());
        pkCnd.setPhotoB(userB.getPhoto());
        //获取A和B选手的总票数
        QueryWrapper<Vote> voteQueryWrapperA = new QueryWrapper<>();
        voteQueryWrapperA.eq("competition_id", group.getGroupId()).eq("user_id", userAId);
        long votesATemp = voteMapper.selectCount(voteQueryWrapperA);
        int votesA = (int) votesATemp;
        QueryWrapper<Vote> voteQueryWrapperB = new QueryWrapper<>();
        voteQueryWrapperB.eq("competition_id", group.getGroupId()).eq("user_id", userBId);
        long votesBTemp = voteMapper.selectCount(voteQueryWrapperB);
        int votesB = (int) votesBTemp;
        pkCnd.setVotesA(votesA);
        pkCnd.setVotesB(votesB);

        return pkCnd;
    }

    /**
     * 获取投票统计页
     *
     * @return 投票统计页
     */
    @RequestMapping("/votescountlist")
    public ModelAndView votescountlist(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "8") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        Page<Count> page = new Page<>(pageNum, pageSize);
        IPage<Count> countIPage = countMapper.selectPage(page, null);

        modelAndView.addObject("counts", countIPage.getRecords());
        modelAndView.addObject("currentPage", countIPage.getCurrent());
        modelAndView.addObject("totalPages", countIPage.getPages());

        modelAndView.setViewName("/votescountlist");
        return modelAndView;
    }

    /**
     * 获取投票统计总数量
     *
     * @return 获取投票统计总数量
     */
    @RequestMapping("/user/getTotalCountVotes")
    @ResponseBody
    public Integer getTotalCountVotes() {
        return Math.toIntExact(countMapper.selectCount(null));
    }

    /**
     * 观众投票页面
     *
     * @return 观众投票页面
     */
    @RequestMapping("/votepage")
    public String votepage() {
        return "/votepage";
    }

    /**
     * 评委投票页面
     *
     * @return 评委投票页面
     */
    @RequestMapping("/votepagejudge")
    public String votepagejudge() {
        return "/votepagejudge";
    }

    /**
     * 获取评委投票的方法
     * (评委10票)
     */
    @GetMapping("/getScores")
    @ResponseBody
    public void getScores(HttpServletRequest request, Authentication authentication) throws ParseException {
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date voteTime = queueDateFormat.parse(time);
        org.springframework.security.core.userdetails.User auth = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", auth.getUsername());
        User judge = userMapper.selectOne(queryWrapper);
        Integer voterId = judge.getUserId();
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        Integer groupId = Integer.valueOf(request.getParameter("groupId"));

        for (int n = 0; n < 10; n++) {
            Vote vote = new Vote();
            vote.setVoterId(voterId);
            vote.setUserId(userId);
            vote.setVoteTime(voteTime);
            vote.setCompetitionId(groupId);
            voteMapper.insert(vote);
        }
    }

    /**
     * 对战控制页面
     *
     * @return 对战控制页面
     */
    @RequestMapping("/battlecontrol")
    public String battlecontrol() {
        return "/battlecontrol";
    }

    /**
     * 开始对战（投票开始）
     *
     * @return 开始对战
     */
    @RequestMapping("/startPK")
    public String startPK(Integer groupId) throws ParseException {
        System.out.println("开始：" + groupId);
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date startTime = queueDateFormat.parse(time);
        UpdateWrapper<Group> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("group_status", 1)
                .set("start_time", startTime)
                .eq("group_id", groupId);
        groupMapper.update(null, updateWrapper);

        return "/battlecontrol";
    }

    /**
     * 结束对战（投票结束）
     *
     * @return 结束对战
     */
    @RequestMapping("/endPK")
    public String endPK(Integer groupId, Integer userAId, Integer userBId) throws ParseException {
        System.out.println("结束：" + groupId);
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date endTime = queueDateFormat.parse(time);
        UpdateWrapper<Group> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("group_status", 2)
                .set("end_time", endTime)
                .eq("group_id", groupId);
        groupMapper.update(null, updateWrapper);
        //查询并处理选手A总票数
        QueryWrapper<Vote> queryWrapperA = new QueryWrapper<>();
        queryWrapperA.eq("competition_id", groupId);
        queryWrapperA.eq("user_id", userAId);
        long userAVotesTemp = voteMapper.selectCount(queryWrapperA);
        int userAVotes = (int) userAVotesTemp;
        //查询并处理选手B总票数
        QueryWrapper<Vote> queryWrapperB = new QueryWrapper<>();
        queryWrapperB.eq("competition_id", groupId);
        queryWrapperB.eq("user_id", userBId);
        long userBVotesTemp = voteMapper.selectCount(queryWrapperB);
        int userBVotes = (int) userBVotesTemp;
        //比较结果
        if (userAVotes == userBVotes) {
            System.out.println("平局");
            System.out.println("不做晋级处理，下次分组再战！");
        } else if (userAVotes > userBVotes) {
            //如果A比B票数高，设置B淘汰。
            UpdateWrapper<User> updateWrapperA = new UpdateWrapper<>();
            updateWrapperA.set("promotion", 1).eq("user_id", userBId);
            userMapper.update(null, updateWrapperA);
        } else {
            //如果A比B票数低，设置A淘汰。
            UpdateWrapper<User> updateWrapperB = new UpdateWrapper<>();
            updateWrapperB.set("promotion", 1).eq("user_id", userAId);
            userMapper.update(null, updateWrapperB);
        }
        //遍历评委id
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 2);
        List<User> judges = userMapper.selectList(queryWrapper);
        List<Integer> ids = judges.stream().map(User::getUserId).collect(Collectors.toList());
        //根据评委id查出每位评委给不同选手的投票数
        int judgesVotesA = 0;
        int judgesVotesB = 0;
        for (int id : ids) {
            QueryWrapper<Vote> idQueryWrapperA = new QueryWrapper<>();
            QueryWrapper<Vote> idQueryWrapperB = new QueryWrapper<>();
            idQueryWrapperA.eq("voter_id", id).eq("competition_id", groupId).eq("user_id", userAId);
            idQueryWrapperB.eq("voter_id", id).eq("competition_id", groupId).eq("user_id", userBId);
            long voteTempA = voteMapper.selectCount(idQueryWrapperA);
            long voteTempB = voteMapper.selectCount(idQueryWrapperB);
            int voteA = (int) voteTempA;
            int voteB = (int) voteTempB;
            judgesVotesA += voteA;
            judgesVotesB += voteB;
            System.out.println("------------------------");
            System.out.println("judgesVotesA" + judgesVotesA);
            System.out.println("judgesVotesB" + judgesVotesB);
            System.out.println("------------------------");
        }
        //计算每位选手的观众投票数
        int audienceVotesA = userAVotes - judgesVotesA;
        int audienceVotesB = userBVotes - judgesVotesB;
        //记录本组比赛结果
        Count countA = new Count();
        countA.setUserId(userAId);
        countA.setUserVotes(audienceVotesA);
        countA.setUserScore(judgesVotesA);
        countA.setUserTotal(userAVotes);
        countA.setCountTime(endTime);
        countA.setCompetitionId(groupId);
        countMapper.insert(countA);
        Count countB = new Count();
        countB.setUserId(userBId);
        countB.setUserVotes(audienceVotesB);
        countB.setUserScore(judgesVotesB);
        countB.setUserTotal(userBVotes);
        countB.setCountTime(endTime);
        countB.setCompetitionId(groupId);
        countMapper.insert(countB);

        return "/battlecontrol";
    }

    /**
     * 观众获取可以投票的组方法
     *
     * @return 观众投票页面
     */
    @GetMapping("/getLatestPK")
    @ResponseBody
    public PKCnd getLatestPK() {
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_status", 1);
        Group group = groupMapper.selectOne(queryWrapper);
        Integer userAId = group.getUserAId();
        Integer userBId = group.getUserBId();
        User userA = userMapper.selectById(userAId);
        User userB = userMapper.selectById(userBId);
        PKCnd pkCnd = new PKCnd();
        pkCnd.setGroupId(group.getGroupId());
        pkCnd.setUserAId(userAId);
        pkCnd.setUserBId(userBId);
        pkCnd.setUsernameA(userA.getUsername());
        pkCnd.setUsernameB(userB.getUsername());
        pkCnd.setPhotoA(userA.getPhoto());
        pkCnd.setPhotoB(userB.getPhoto());
        return pkCnd;
    }

    /**
     * 获取观众投票的方法
     */
    @GetMapping("/getVotes")
    @ResponseBody
    public void getVotes(HttpServletRequest request) throws ParseException {

        HttpSession session = request.getSession();
        String sessionId = session.getId();
        String sessionIdNum = Method.getRandomNumericString(Method.convertToNumericString(sessionId), 6);
        int voterId = Integer.parseInt(sessionIdNum);
        int userId = Integer.parseInt(request.getParameter("userId"));
        int groupId = Integer.parseInt(request.getParameter("groupId"));
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date voteTime = queueDateFormat.parse(time);
        Vote vote = new Vote();
        vote.setVoterId(voterId);
        vote.setUserId(userId);
        vote.setVoteTime(voteTime);
        vote.setCompetitionId(groupId);
        voteMapper.insert(vote);
    }

    /**
     * 用户登录页
     *
     * @return 用户登录页
     */
    @RequestMapping("/user/login")
    public String login() {
        return "login/login";
    }

    /**
     * 歌手报名信息页
     *
     * @param authentication 权限
     * @return 歌手报名信息页
     */
    @RequestMapping("/singer")
    public ModelAndView singer(Authentication authentication) {
        org.springframework.security.core.userdetails.User users = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
        ModelAndView modelAndView = new ModelAndView();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, users.getUsername());
        User userDetail = userMapper.selectOne(queryWrapper);
        modelAndView.addObject("userDetail", userDetail);
        modelAndView.setViewName("singer");
        return modelAndView;
    }

    /**
     * 歌手报名信息修改（重新报名）
     *
     * @param userId             用户编号
     * @param user               用户实体
     * @param myfile             文件
     * @param httpServletRequest 客户端的请求
     * @return 主页面
     * @throws Exception 异常
     */
    @RequestMapping("/user/doUserUpdate")
    public ModelAndView doUserUpdate(Integer userId, User user, MultipartFile myfile, HttpServletRequest httpServletRequest) throws Exception {
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date updateTime = queueDateFormat.parse(time);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(hashedPassword);
        user.setUserId(userId);
        user.setUpdate_time(updateTime);
        user.setRoleId(3);
        user.setStatus(3);
        ModelAndView modelAndView = new ModelAndView();
        String realPath = httpServletRequest.getServletContext().getRealPath("uploadfiles");
        String fakePath = String.valueOf(new File("E:\\My_Project_Place\\IDEA_Project\\springboot02\\votesys\\src\\main\\resources\\static\\source\\uploadfiles"));
        System.out.println("服务器文件的上传路径为：" + realPath);
        System.out.println("项目文件的上传路径为：" + fakePath);
        String filename = UUID.randomUUID().toString() + "-" + myfile.getOriginalFilename();
        httpServletRequest.setAttribute("filename", filename);
        File targetFile = new File(realPath, filename);
        File targetFakeFile = new File(fakePath, filename);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        myfile.transferTo(targetFakeFile);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("username", user.getUsername())
                .set("password", user.getPassword())
                .set("phone", user.getPhone())
                .set("photo", filename)
                .set("status", user.getStatus())
                .set("role_id", user.getRoleId())
                .set("update_time", user.getUpdate_time())
                .eq("user_id", userId);
        userMapper.update(null, updateWrapper);
        modelAndView.setViewName("redirect:/main");
        return modelAndView;
    }

    /**
     * 歌手报名页
     *
     * @return 歌手报名页
     */
    @RequestMapping("/user/register")
    public String register() {
        return "register/register";
    }

    /**
     * 注册方法
     *
     * @param user               用户实体
     * @param myfile             文件
     * @param httpServletRequest 客户端请求
     * @return 报名成功页
     * @throws IOException    输入输出异常
     * @throws ParseException 解析异常
     */
    @RequestMapping("/user/doRegister")
    public ModelAndView doRegister(User user, MultipartFile myfile, HttpServletRequest httpServletRequest) throws IOException, ParseException {
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date createTime = queueDateFormat.parse(time);
        ModelAndView modelAndView = new ModelAndView();
        String username = httpServletRequest.getParameter("username");
        String tel = httpServletRequest.getParameter("phone");
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        user.setRoleId(3);
        user.setStatus(3);
        user.setPromotion(0);
        user.setUsername(username);
        user.setPhone(tel);
        user.setCreateTime(createTime);
        String realPath = httpServletRequest.getServletContext().getRealPath("uploadfiles");
        String fakePath = String.valueOf(new File("E:\\My_Project_Place\\IDEA_Project\\springboot02\\votesys\\src\\main\\resources\\static\\source\\uploadfiles"));
        System.out.println("服务器文件的上传路径为：" + realPath);
        System.out.println("项目文件的上传路径为：" + fakePath);
        String filename = UUID.randomUUID().toString() + "-" + myfile.getOriginalFilename();
        httpServletRequest.setAttribute("filename", filename);
        File targetFile = new File(realPath, filename);
        File targetFakeFile = new File(fakePath, filename);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        myfile.transferTo(targetFakeFile);
        user.setPhoto(filename);
        userMapper.insert(user);
        modelAndView.setViewName("register/register_succ");
        return modelAndView;
    }

    /**
     * 新增裁判方法
     *
     * @param user               用户实体
     * @param myfile             文件
     * @param httpServletRequest 客户端请求
     * @return 裁判列表
     * @throws IOException    输入输出异常
     * @throws ParseException 解析异常
     */
    @RequestMapping("/doAddJudge")
    public ModelAndView doAddJudge(User user, MultipartFile myfile, HttpServletRequest httpServletRequest) throws IOException, ParseException {
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date createTime = queueDateFormat.parse(time);
        ModelAndView modelAndView = new ModelAndView();
        String username = httpServletRequest.getParameter("username");
        String tel = httpServletRequest.getParameter("phone");
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        user.setRoleId(2);
        user.setStatus(0);
        user.setUsername(username);
        user.setPhone(tel);
        user.setCreateTime(createTime);
        String realPath = httpServletRequest.getServletContext().getRealPath("uploadfiles");
        String fakePath = String.valueOf(new File("E:\\My_Project_Place\\IDEA_Project\\springboot02\\votesys\\src\\main\\resources\\static\\source\\uploadfiles"));
        System.out.println("服务器文件的上传路径为：" + realPath);
        System.out.println("项目文件的上传路径为：" + fakePath);
        String filename = UUID.randomUUID().toString() + "-" + myfile.getOriginalFilename();
        httpServletRequest.setAttribute("filename", filename);
        File targetFile = new File(realPath, filename);
        File targetFakeFile = new File(fakePath, filename);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        myfile.transferTo(targetFakeFile);
        user.setPhoto(filename);
        userMapper.insert(user);
        modelAndView.setViewName("redirect:/user/doJudgeList");
        return modelAndView;
    }

    /**
     * 新增工作人员方法
     *
     * @param user               用户实体
     * @param myfile             文件
     * @param httpServletRequest 客户端请求
     * @return 工作人员列表
     * @throws IOException    输入输出异常
     * @throws ParseException 解析异常
     */
    @RequestMapping("/doAddStuff")
    public ModelAndView doAddStuff(User user, MultipartFile myfile, HttpServletRequest httpServletRequest) throws IOException, ParseException {
        SimpleDateFormat queueDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String time = queueDateFormat.format(new Date());
        Date createTime = queueDateFormat.parse(time);
        ModelAndView modelAndView = new ModelAndView();
        String username = httpServletRequest.getParameter("username");
        String tel = httpServletRequest.getParameter("phone");
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        user.setRoleId(1);
        user.setStatus(0);
        user.setUsername(username);
        user.setPhone(tel);
        user.setCreateTime(createTime);
        System.out.println(user);
        String realPath = httpServletRequest.getServletContext().getRealPath("uploadfiles");
        String fakePath = String.valueOf(new File("E:\\My_Project_Place\\IDEA_Project\\springboot02\\votesys\\src\\main\\resources\\static\\source\\uploadfiles"));
        System.out.println("服务器文件的上传路径为：" + realPath);
        System.out.println("项目文件的上传路径为：" + fakePath);
        String filename = UUID.randomUUID().toString() + "-" + myfile.getOriginalFilename();
        httpServletRequest.setAttribute("filename", filename);
        File targetFile = new File(realPath, filename);
        File targetFakeFile = new File(fakePath, filename);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        myfile.transferTo(targetFakeFile);
        user.setPhoto(filename);
        userMapper.insert(user);
        modelAndView.setViewName("redirect:/user/doStuffList");
        return modelAndView;
    }

    /**
     * 新增裁判页
     *
     * @return 新增裁判页
     */
    @RequestMapping("/addjudge")
    public String addjudge() {
        return "addjudge";
    }

    /**
     * 新增工作人员页
     *
     * @return 新增工作人员页
     */
    @RequestMapping("/addstuff")
    public String addstuff() {
        return "addstuff";
    }

    /**
     * 歌手列表页
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 歌手列表页
     */
    @RequestMapping("/user/doSingerList")
    public ModelAndView doSingerList(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "3") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 3);
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
        modelAndView.addObject("users", userPage.getRecords());
        modelAndView.addObject("currentPage", userPage.getCurrent());
        modelAndView.addObject("totalPages", userPage.getPages());
        modelAndView.setViewName("singerlist");
        return modelAndView;
    }

    /**
     * 裁判列表页
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 裁判列表页
     */
    @RequestMapping("/user/doJudgeList")
    public ModelAndView doJudgeList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "3") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 2);
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
        modelAndView.addObject("users", userPage.getRecords());
        modelAndView.addObject("currentPage", userPage.getCurrent());
        modelAndView.addObject("totalPages", userPage.getPages());
        modelAndView.setViewName("judgelist");
        return modelAndView;
    }

    /**
     * 工作人员页
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 工作人员页
     */
    @RequestMapping("/user/doStuffList")
    public ModelAndView doStuffList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "3") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 1);
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
        modelAndView.addObject("users", userPage.getRecords());
        modelAndView.addObject("currentPage", userPage.getCurrent());
        modelAndView.addObject("totalPages", userPage.getPages());
        modelAndView.setViewName("stufflist");
        return modelAndView;
    }

    /**
     * 未审核歌手列表方法
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 未审核歌手列表
     */
    @RequestMapping("/unreviewed")
    public ModelAndView unreviewed(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "3") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 3).eq("role_id", 3);
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
        modelAndView.addObject("users", userPage.getRecords());
        modelAndView.addObject("currentPage", userPage.getCurrent());
        modelAndView.addObject("totalPages", userPage.getPages());
        modelAndView.setViewName("unreviewedlist");
        return modelAndView;
    }

    /**
     * 通过歌手列表方法
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 通过歌手列表
     */
    @RequestMapping("/passed")
    public ModelAndView passed(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "3") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1).eq("role_id", 3);
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
        modelAndView.addObject("users", userPage.getRecords());
        modelAndView.addObject("currentPage", userPage.getCurrent());
        modelAndView.addObject("totalPages", userPage.getPages());
        modelAndView.setViewName("passedlist");
        return modelAndView;
    }

    /**
     * 未通过歌手列表方法
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 未通过歌手列表
     */
    @RequestMapping("/unsanctioned")
    public ModelAndView unsanctioned(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "3") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 2).eq("role_id", 3);
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
        modelAndView.addObject("users", userPage.getRecords());
        modelAndView.addObject("currentPage", userPage.getCurrent());
        modelAndView.addObject("totalPages", userPage.getPages());
        modelAndView.setViewName("unsanctionedlist");
        return modelAndView;
    }

    /**
     * 歌手总数方法
     *
     * @return 歌手总数
     */
    @RequestMapping("/user/getTotalCountSinger")
    @ResponseBody
    public Integer getTotalCountSinger() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 3);
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    /**
     * 裁判总数方法
     *
     * @return 裁判总数
     */
    @RequestMapping("/user/getTotalCountJudge")
    @ResponseBody
    public Integer getTotalCountJudge() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 2);
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    /**
     * 工作人员总数方法
     *
     * @return 工作人员总数
     */
    @RequestMapping("/user/getTotalCountStuff")
    @ResponseBody
    public Integer getTotalCountStuff() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 1);
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    /**
     * 待审歌手总数方法
     *
     * @return 待审歌手总数
     */
    @RequestMapping("/user/getTotalCountUnreviewed")
    @ResponseBody
    public Integer getTotalCountUnreviewed() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 3).eq("role_id", 3);
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    /**
     * 通过歌手总数方法
     *
     * @return 通过歌手总数
     */
    @RequestMapping("/user/getTotalCountPassed")
    @ResponseBody
    public Integer getTotalCountPassed() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1).eq("role_id", 3);
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    /**
     * 未通过歌手总数方法
     *
     * @return 未通过歌手总数
     */
    @RequestMapping("/user/getTotalCountUnsanctioned")
    @ResponseBody
    public Integer getTotalCountUnsanctioned() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 2).eq("role_id", 3);
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    /**
     * 通过歌手方法
     *
     * @param userId 歌手编号
     * @return 歌手列表
     */
    @RequestMapping("/pass")
    public ModelAndView pass(Integer userId) {
        ModelAndView modelAndView = new ModelAndView();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 1).eq("user_id", userId);
        userMapper.update(null, updateWrapper);
        modelAndView.setViewName("redirect:/user/doSingerList");
        return modelAndView;
    }

    /**
     * 不通过歌手方法
     *
     * @param userId 歌手编号
     * @return 歌手列表
     */
    @RequestMapping("/fail")
    public ModelAndView fail(Integer userId) {
        ModelAndView modelAndView = new ModelAndView();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 2).eq("user_id", userId);
        userMapper.update(null, updateWrapper);
        modelAndView.setViewName("redirect:/user/doSingerList");
        return modelAndView;
    }

    /**
     * 删除歌手方法
     *
     * @param userId 歌手编号
     * @return 歌手列表
     */
    @RequestMapping("/deleteSinger")
    public ModelAndView delete(Integer userId) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        userMapper.delete(queryWrapper);
        modelAndView.setViewName("redirect:/user/doSingerList");
        return modelAndView;
    }

    /**
     * 删除评委方法
     *
     * @param userId 评委编号
     * @return 评委列表
     */
    @RequestMapping("/deleteJudge")
    public ModelAndView deleteJudge(Integer userId) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        userMapper.delete(queryWrapper);
        modelAndView.setViewName("redirect:/user/doJudgeList");
        return modelAndView;
    }

    /**
     * 删除工作人员方法
     *
     * @param userId 工作人员编号
     * @return 工作人员列表
     */
    @RequestMapping("/deleteStuff")
    public ModelAndView deleteStuff(Integer userId) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        userMapper.delete(queryWrapper);
        modelAndView.setViewName("redirect:/user/doStuffList");
        return modelAndView;
    }
}
